package trabajoISW2.data;

import java.util.List;

import javax.persistence.Query;

import trabajoISW2.domain.implementations.Address;

public class AddressDAO extends BaseDAO implements IAddressDAO {
	
	
	public AddressDAO(){
		
	}

	public Address createAddress(Address ad) {
		getEm().getTransaction().begin();
		getEm().persist(ad);
		getEm().getTransaction().commit();
		getEm().close();
		return ad;
	}

	public void deleteAddress(final int id) {
		final Address a = getAddress(id);
		if (a != null) {
			getEm().getTransaction().begin();
			getEm().remove(a);
			getEm().getTransaction().commit();
			getEm().close();


		}

	}

	public void updateAddress(Address a) {
		getEm().getTransaction().begin();

		getEm().merge(a);
		getEm().getTransaction().commit();
		getEm().close();
	}

	public Address getAddress(int id) {

		Address a = getEm().find(Address.class, id);
		return a;

	}
	
	public List<Address> getAddresses() {

		Query q = getEm().createQuery("Select a FROM Address a");
		return q.getResultList();

	}	
	

	

}
